import Vue from 'vue'
import VueRouter from 'vue-router'
import loginRegister from '../views/loginRegister.vue'
import NewsManagement from '../views/NewsManagement.vue'
import mainlayout from '../components/mainlayout.vue'
import NewsEdit from '../views/NewsEdit.vue'
import NewsSearch from '../views/searchNews.vue'
import topicManagement from '../views/topicManagement.vue'
import morenews1topic from '../views/morenews1topic.vue'
import NewsScan from '../views/NewsScan.vue'
import userManagement from '../views/userManagement.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: loginRegister
  },
  {
    path: '/mainlayout',
    name: 'mainlayout',
    component: mainlayout,
    children: [
      {
        path: 'newsManagement',
        component: NewsManagement,
        name:'newsManagement'
      },
      {
        path: 'NewsEdit',
        name: 'NewsEdit',
        component: NewsEdit
      },
      {
        path: 'NewsScan',
        name: 'NewsScan',
        component: NewsScan
      },
      {
        path: 'NewsSearch',
        name: 'NewsSearch',
        component: NewsSearch
      },
      {
        path: 'topicManagement',
        name: 'topicManagement',
        component: topicManagement
      },
      {
        path: 'morenews1topic',
        name: 'morenews1topic',
        component: morenews1topic
      },
      {
        path: 'userManagement',
        name: 'userManagement',
        component: userManagement
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    let token = window.sessionStorage.getItem('token')
    console.log('token:', token)
    if (token === null || token === '') {
      next('/');
    } else {
      next();
    }
  }
});

export default router
